package com.pirestupppai.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.pirestupppai.R;
import com.pirestupppai.bean.FeedItem;
import com.pirestupppai.ui.activity.MessageListActivity;
import com.pirestupppai.widgets.view.FeedView;

import java.util.List;

/**
 * 动态列表适配
 *
 * @author: xiaoluo
 * @date: 2017-03-06 16:30
 */
public class FeedListAdapter extends RecyclerView.Adapter<FeedListAdapter.MyViewHolder> {
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;
    private static final int TYPE_HEADER = 2;
    private static final int TYPE_RECOMMEND = 3;

    private Context mContext;
    private List<FeedItem> mList;

    private View mFooterView;
    private View mHeaderView;
    private View mRecommendView;
    private ImageView mMessageIv;
    private ImageView mDotIv;

    public FeedListAdapter(Context context, List<FeedItem> list) {
        this.mContext = context;
        this.mList = list;

        mHeaderView = LayoutInflater.from(mContext).inflate(R.layout.view_feed_header, null);
        mMessageIv = (ImageView) mHeaderView.findViewById(R.id.feed_message_iv);
        mDotIv = (ImageView) mHeaderView.findViewById(R.id.feed_message_dot);

        mMessageIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDotIv.setVisibility(View.GONE);
                mContext.startActivity(new Intent(mContext, MessageListActivity.class));
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_HEADER;
        }
        if (mRecommendView == null) {
            if (position == (mList.size() + 1)) {
                return TYPE_FOOTER;
            }
        }

        if (mRecommendView != null) {
            if (position == (mList.size() + 1)) {
                return TYPE_FOOTER;
            }
            if (mList.size() >= 2) {
                if (position == 3) {
                    return TYPE_RECOMMEND;
                }
            } else if (mList.size() == 1) {
                if (position == 2) {
                    return TYPE_RECOMMEND;
                }
            } else {
                return TYPE_RECOMMEND;
            }
        }
        return TYPE_ITEM;
    }

    @Override
    public FeedListAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mFooterView != null && viewType == TYPE_FOOTER) {
            return new MyViewHolder(mFooterView);
        }
        if (mHeaderView != null && viewType == TYPE_HEADER) {
            return new MyViewHolder(mHeaderView);
        }
        if (mRecommendView != null && viewType == TYPE_RECOMMEND) {
            return new MyViewHolder(mRecommendView);
        }

        FeedView view = new FeedView(mContext);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(FeedListAdapter.MyViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_RECOMMEND || getItemViewType(position) == TYPE_FOOTER || getItemViewType(position) == TYPE_HEADER) {
            return;
        }
        int realPosition = getRealPosition(position);
        holder.setData(realPosition);
    }

    /**
     * 获取列表子项实际位置
     */
    private int getRealPosition(int position) {
        if (position <= 2) {
            return position - 1;
        } else {
            return mRecommendView == null ? position - 1 : position - 2;
        }
    }

    @Override
    public int getItemCount() {
        if (mRecommendView != null) {
            return mList.size() + 3;
        } else {
            return mList.size() + 2;
        }
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        FeedView feedView;

        public MyViewHolder(View itemView) {
            super(itemView);
            if (itemView == mRecommendView) {
                return;
            }
            if (itemView == mFooterView) {
                return;
            }

            if (itemView == mHeaderView) {
                return;
            }
            feedView = (FeedView) itemView;
        }

        void setData(int position) {
            feedView.setViews(mList.get(position), FeedView.ShowType.SHOW_TYPE_LIST);
        }
    }

    public void setFooterView(View footerView) {
        mFooterView = footerView;
    }

    public void setRecommendView(View recommendView) {
        mRecommendView = recommendView;
    }
}
